package com.qifan.energy.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import com.orhanobut.logger.Logger;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;

/*
* 活动堆栈管理工具类
* 静态方法
* */
public class ActivityStackUtil {

    private static Stack<Activity> activityStack = new Stack<Activity>();

    //单一实例
    /*private static ActivityStackUtil instance;
    private ActivityStackUtil(){}
    public static ActivityStackUtil getActivityUtil(){
        if(instance==null){
            instance = new ActivityStackUtil();
        }
        return instance;
    }*/

    /**
     * 添加Activity到堆栈
     */
    public static void addActivity(Activity activity){
        activityStack.add(activity);
    }

    /**
     * 得到指定类名的Activity
     */
    public static Activity getActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }

    /**
     * 获取栈顶Activity（堆栈中最后一个压入的）
     */
    public static Activity getTopActivity() {
        if(activityStack.isEmpty()){
            return null;
        } else{
            return activityStack.lastElement();
        }
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public static Activity currentActivity(){
        if(activityStack.isEmpty()){
            return null;
        } else{
            return activityStack.lastElement();
        }
    }

    /**
     * 结束栈顶Activity（堆栈中最后一个压入的）
     */
    public static void finishTopActivity() {
        if(!activityStack.isEmpty()){
            Activity activity = activityStack.lastElement();
            finishActivity(activity);
        }
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public static void finishActivity(){
        if (!activityStack.isEmpty()){
            Activity activity = activityStack.lastElement();
            if(activity!=null){
                activity.finish();
                activity=null;
            }
        }
    }

    /**
     * 结束指定的Activity
     */
    public static void finishActivity(Activity activity){
        if(activity!=null){
            activityStack.remove(activity);
            activity.finish();
            activity=null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public static void finishActivity(Class<?> cls){
        Iterator iterator = activityStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = (Activity) iterator.next();
            if (activity.getClass().equals(cls)) {
                iterator.remove();
                activity.finish();
                break;
            }
        }
    }

    /**
     * 结束到指定类名的Activity为止（此方法慎用 容易销毁所有Activity）
     */
    public static void finishToActivity(Class<?> cls){
        ListIterator iterator = activityStack.listIterator();
        while(iterator.hasNext()) {//必须先正向迭代
            Logger.i("iterator.hasNext",iterator.next());
        }
        while (iterator.hasPrevious()) {
            Activity activity = (Activity) iterator.previous();
            iterator.remove();
            activity.finish();
            if (activity.getClass().equals(cls)) {
                break;
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public static void finishAllActivity(){
        for (int i = 0, size = activityStack.size(); i < size; i++){
            if (null != activityStack.get(i)){
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public static void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
            /*activityMgr.restartPackage(context.getPackageName());*/
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {

        }
    }
}
